package org.song.service.impl;

import org.song.mapper.LinkMapper;
import org.song.model.dto.LinkDto;
import org.song.model.vo.Link;
import org.song.service.LinkService;
import org.song.service.impl.common.AbstractBaseServiceImpl;
import org.song.util.JacksonUtil;
import org.song.util.SnowflakeUtil;
import org.song.util.email.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LinkServiceImpl extends AbstractBaseServiceImpl implements LinkService {

    @Autowired( required = true )
    @Qualifier( value = "linkMapper")
    private LinkMapper linkMapper;

    @Autowired( required = true )
    @Qualifier( value = "emailUtil")
    private EmailUtil emailUtil;

    @Override
    public List<Link> listLinks( @NonNull LinkDto linkDto) {
        return linkMapper.listLinks(linkDto);
    }


    @Override
    public int deleteLink( @NonNull Link link) {
        return linkMapper.deleteLink(link);
    }

    @Override
    public int insertLink( @NonNull Link link) {
        link.setId( SnowflakeUtil.getNextLongId() );
        log.info("友链申请：" + JacksonUtil.getJsonString( link ));
        emailUtil.sendEasyEmail("友链申请\n" , JacksonUtil.getJsonString( link ));
        return linkMapper.insertLink(link);
    }

    @Override
    public int updateLink( @NonNull Link link) {
        return linkMapper.updateLink(link);
    }

}
